using WinBookKeeping.Utility;
using WinBookKeeping.Data;

namespace WinBookKeeping
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Normal;
            this.FormBorderStyle = FormBorderStyle.Sizable;
            this.MinimumSize = new Size(1800, 800); // ������С�ߴ�
            this.IsMdiContainer = true;
        }

        private void btnItems_Click(object sender, EventArgs e)
        {
            FrmItemManage frmItemManage = new FrmItemManage();
            //frmItemManage.MdiParent=this;
            //frmItemManage.StartPosition = FormStartPosition.CenterScreen;
            //frmItemManage.Show();
            ShowForm(frmItemManage);
        }
        private void btnIncome_Click(object sender, EventArgs e)
        {
            FrmIncomeManage frmIncomeManage = new FrmIncomeManage();
            ShowForm(frmIncomeManage);

        }
        private void btnExpend_Click(object sender, EventArgs e)
        {
            FrmExpendManage frmExpend = new FrmExpendManage();
            ShowForm(frmExpend);
        }
        private void btnMonthStatistics_Click(object sender, EventArgs e)
        {
            FrmMonthStatisics frmMonthStat = new FrmMonthStatisics();
            ShowForm(frmMonthStat);
        }
        private void btnWeekStatistics_Click(object sender, EventArgs e)
        {
            FrmWeekStatistics frmWeekStatistics = new FrmWeekStatistics();
            ShowForm(frmWeekStatistics);
        }
        private void btnDayStatistics_Click(object sender, EventArgs e)
        {
            FrmDayStatisitics frmDayStat = new FrmDayStatisitics();
            ShowForm(frmDayStat);
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = MessageBox.Show(
                "是否确认退出系统？",
                "确认退出",
                MessageBoxButtons.YesNo,  // ʹ�� Yes/No ��ť
                MessageBoxIcon.Question
            );

            if (dialogResult == DialogResult.Yes) // �û�������ǡ�
            {
                Application.Exit();
            }
            // �����Զ�ȡ��
        }
        private void ShowForm(Form form)
        {
            // ��鴰���Ƿ��Ѵ���
            foreach (Form f in Application.OpenForms)
            {
                if (f.Name == form.Name)
                {
                    f.Activate();
                    ResizeChildForm(f);
                    return;
                }
            }

            // �´�������
            form.MdiParent = this;
            form.FormBorderStyle = FormBorderStyle.None; // �Ƴ��߿�
            form.StartPosition = FormStartPosition.Manual;
            form.Show();
            ResizeChildForm(form);
        }

        private void ResizeChildForm(Form childForm)
        {
           
            int topOffset = 0;
            if (this.MainMenuStrip != null) topOffset += this.MainMenuStrip.Height;

            childForm.Location = new Point(0, topOffset);
            childForm.Size = new Size(this.ClientSize.Width, this.ClientSize.Height - topOffset);

           
            childForm.PerformLayout();
        }
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            foreach (Form childForm in this.MdiChildren)
            {
                ResizeChildForm(childForm);
            }
        }
        private void FrmMain_Load(object sender, EventArgs e)
        {
            // 数据库初始化
            using (var context = new BookKeepingContext())
            {
                context.Database.EnsureCreated();
            }
        }

        private void buttonAI_Click(object sender, EventArgs e)
        {
            FrmAI ai=new FrmAI();
           ShowForm(ai);
        }
    }
}
